﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// FuncView.xaml 的交互逻辑
    /// </summary>
    public partial class FuncView : UserControl
    {
        public ViewModels.FuncViewModel viewModel =new ViewModels.FuncViewModel();
        public FuncView(Order order = null)
        {
            InitializeComponent();
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out FuncEnum.TestMode mode);
                Func parameter = JsonConvert.DeserializeObject<Func>(order.deviceModel);
                switch (mode)
                {
                    case FuncEnum.TestMode.延时函数:
                        viewModel.MenuIndex = 0;
                        viewModel.DelayTime = parameter.延时时长;
                        break;
                    case FuncEnum.TestMode.计算函数:
                        viewModel.MenuIndex = 1;
                        viewModel.FuncStr = parameter.计算表达式;
                        viewModel.FuncMax = parameter.上限值?.ToString();
                        viewModel.FuncMin = parameter.下限值?.ToString();
                        viewModel.FuncUnit = parameter.结果单位;
                        break;
                    case FuncEnum.TestMode.取值函数:
                        viewModel.MenuIndex = 2;
                        viewModel.TestIndex = parameter.测试项序号;
                        viewModel.TestNum = parameter.函数编号;
                        break;
                    case FuncEnum.TestMode.循环函数:
                        viewModel.MenuIndex = 3;
                        viewModel.LoopDevice = parameter.循环设备;
                        viewModel.LoopFunc = parameter.循环方式;
                        viewModel.LoopTimes = parameter.最大循环次数;
                        viewModel.LoopBreak = parameter.跳出方式;
                        break;
                    case FuncEnum.TestMode.人机交互:
                        viewModel.MenuIndex = 4;
                        viewModel.InfoStr = parameter.交互内容;
                        viewModel.InfoTitle = parameter.提示信息;
                        break;
                    default:
                        break;
                }
            }
            DataContext = viewModel;

        }

        private void DelayClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Func parameter = new Func()
            {
                mode = FuncEnum.TestMode.延时函数,
                延时时长 = viewModel.DelayTime
            };
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder("函数模块"));
            }
        }

        private void FuncClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.FuncMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.FuncMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数     
            Func parameter = new Func()
            {
                mode = FuncEnum.TestMode.计算函数,
                计算表达式 = viewModel.FuncStr,
                上限值 = max,
                下限值 = min,
                结果单位 = viewModel.FuncUnit,
            }; 
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder("函数模块"));
            }
        }
        private void LoopClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Func parameter = new Func()
            {
                mode = FuncEnum.TestMode.循环函数,
                循环设备 = viewModel.LoopDevice,
                循环方式 = viewModel.LoopFunc,
                最大循环次数 = viewModel.LoopTimes,
                跳出方式 = viewModel.LoopBreak,
            };
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder("函数模块"));
            }
        }

        private void InfoClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Func parameter = new Func()
            {
                mode = FuncEnum.TestMode.人机交互,
                交互内容 = viewModel.InfoStr,
                提示信息 = viewModel.InfoTitle
            };
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder("函数模块"));
            }
        }

        private void GetClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Func parameter = new Func()
            {
                mode = FuncEnum.TestMode.取值函数,
                测试项序号 = viewModel.TestIndex,
                函数编号 = viewModel.TestNum
            };
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder("函数模块"));
            }
        }
    }
}
